#include "lcd12864.c"
#include "uart.c"
unsigned int time=0;
unsigned int time_seed;
#define START 1
#define PAUSE 2
#define RIGHT 3
#define LEFT  4
#define UP    5
#define DOWN  6
#define ZERO  0
unsigned int stsss;
int timer_rand()
{
    time_seed = (time * 123 + 255) % 10;

    return time_seed;
}
void timer0_init()
{
    TMOD &= 0xF0;
    TMOD |= 0x01;

    TH0 = 0xDB;

    TL0 = 0xFF;

    TR0 = 1;

    ET0 = 1;

    EA = 1;
    
    PT0 = 1;
}
void timer0_ISR() interrupt 1
{
    
  TH0 = 0xDB;
    
  TL0 = 0xFF;

  time=time+1;

}

int mini;
unsigned char xdata itany_ch[] =
{

0x00,0x02,0x0C,0xC0,0x00,0x20,0xA4,0x64,0xA4,0x3F,0x24,0x64,0xA4,0x20,0x20,0x00,
0x02,0x02,0x7F,0x00,0x80,0x89,0x44,0x46,0x2B,0x12,0x2A,0x46,0x80,0x81,0x00,0x00,

0x10,0x60,0x02,0x0C,0xC0,0x10,0x08,0xF7,0x14,0x54,0x94,0x14,0xF4,0x04,0x00,0x00,
0x04,0x04,0x7C,0x03,0x00,0x01,0x1D,0x13,0x11,0x55,0x99,0x51,0x3F,0x11,0x01,0x00,

0x00,0x08,0x88,0x48,0x28,0x18,0x0F,0xE8,0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00,
0x00,0x20,0x11,0x09,0x05,0x41,0x81,0x7F,0x01,0x01,0x05,0x09,0x11,0x20,0x00,0x00,

};

void demo_display_itany_ch()
{
    // 占用第3、4页，从64列开始写，写中文字符 16x16
    unsigned char i, j;
    unsigned char col = 0;

    for (i = 0; i < sizeof(itany_ch) / sizeof(itany_ch[0]); i += 32)
    {
        // 处理16个byte， 前面8个写入第一页，后面8个写入第二页
        for (j = 0; j < 16; j++)
        {

            lcd_display_one_byte_in_page(0, col + j, itany_ch[i + j]);
            lcd_display_one_byte_in_page(1, col + j, itany_ch[i + j + 16]);
        }
        col += 16;
    }
}
void pig(int x)
{
unsigned char b,c,d;
if(x==0)
{
  for(b=51;b<=54;b++)
  {
  lcd_display_one_byte_in_page(0,b,0xF0);
  }
}

}
void display()
{
   unsigned char i;
   lcd_init();
   lcd_clear_screan();
   demo_display_itany_ch();
   for (i = 0; i < 8; i++)
   {
        lcd_display_one_byte_in_page(i, 50, 0xFF);
        lcd_display_one_byte_in_page(i, 91, 0xFF);
    }

}

int  state()
{
    if(BTN_START==0)
    {
        delay_ms(10);
        if(BTN_START==0)
        {
         while(BTN_START==0){};
         return 1;
        }
    }
    if(BTN_PAUSE==0)
    {
        delay_ms(10);
        if(BTN_PAUSE==0)
        {
         while(BTN_PAUSE==0){};
         return 2;
        } 
    }
    if(BTN_RIGHT==0)
    {
        delay_ms(10);
        if(BTN_RIGHT==0)
        {
            while(BTN_RIGHT==0){};
            return 3;
        }
    }
    if(BTN_LEFT==0)
    {
        delay_ms(10);
        if(BTN_LEFT==0)
        {
            while(BTN_LEFT==0){};
            return 4;
        }
    }
    if(BTN_UP==0)
    {
        delay_ms(10);
        if(BTN_UP==0)
        {
          while(BTN_UP==0){};
          return 5;
        }
    }
    if(BTN_DOWN==0)
    {
        delay_ms(10);
        if(BTN_DOWN==0)
        {
          while (BTN_DOWN==0){};
          return 6;
        }
    }
    return 0;
}

void move()
{
  unsigned int j,k,l;
  stsss = timer_rand();
for(l=0;l<=7;l++)
{
  for(k=0;k<=4;k++)
  {
  for(j=0;j<4;j++)
  {
    lcd_display_one_byte_in_page(l, 51+j+4*stsss, 0x0F<<k);
  }
  delay_ms(50);
  }
  for(k=1;k<=4;k++)
  {
   for(j=0;j<4;j++)
   {
    lcd_display_one_byte_in_page(l,51+j+4*stsss,0xF0<<k);
    lcd_display_one_byte_in_page(l+1,51+j+4*stsss,0x00<<k);
   } 
  }
  delay_ms(50);
}
   for(j=0;j<4;j++)
   {
   lcd_display_one_byte_in_page(7,51+j+4*stsss,0xF0);
   }
}
void main()
{
    int j;
    unsigned char i;
    timer0_init();
    uart_init();
    display();
    while (1)
    {
     j=state();
     if(j==START)
    {
    while(1)
    {
    move();
      switch (mini=state())
     {
     case START:
     printf("1\n");
     break;
     
     case PAUSE:
     printf("2\n");
     break;
     
     case RIGHT:
     stsss=stsss+1;
     printf("3\n");
     break;

     case LEFT:
     stsss=stsss-1;
     printf("4\n");
     break;

     case UP:
     printf("5\n");
     break;

     case DOWN:
     printf("6\n");
     break;

     case ZERO:
     
     break;

     default:

        break;
     }
    }

    }
}
}

